package org.start;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;

import org.jetbrains.annotations.Nullable;

public class Io {

    // 获取当前程序的根目录
    static final Path ROOT_PATH = Paths.get(System.getProperty("user.dir"));

    // 使用 BufferedReader 在控制台读取输入流
    static void testBufferedReader() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        try {
            String tempLine = "";
            do {
                tempLine = br.readLine();
                str += tempLine;
            } while (!tempLine.equals("$fqmyysjjd"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 文件IO流
    static void testFileStreamByBuffer(String tempName) {
        Path targetDir = ROOT_PATH.resolve("start/doc");
        System.out.println(targetDir);
        File directory = new File(targetDir.toString());
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    try {
                        String result = readFileByBufferReader(file);
                        String newName = tempName + "__" + file.getName();
                        writeFileByBufferWriter(targetDir.resolve("test").resolve(Paths.get(newName)), result);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }

    // 如果不想在方法内处理异常，也可以抛出，让上层调用方处理
    static String readFileByBufferReader(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        System.out.println(file.getName());
        String res = "";
        String tempLine = "";
        while (tempLine != null) {
            res += tempLine + '\n';
            tempLine = br.readLine();
        }
        return res;
    }

    static void writeFileByBufferWriter(Path path, String content) throws IOException {
        // 使用try的 () 包裹语句生产读写器，并在 {} 内使用，会自动关闭文件读写器
        try (FileWriter fw = new FileWriter(path.toFile());
             BufferedWriter bw = new BufferedWriter(fw)) {
            bw.write(content);
        }
    }


    // 通过Files写入
    static void testFileStreamByFiles(String tempName) {
        Path targetDir = ROOT_PATH.resolve("start").resolve("doc");
        File directory = new File(targetDir.toString());
        File[] files = directory.listFiles();
        for (File file : files) {
            try {
                if (file.isFile()) {
                    Path rTargetPath = Paths.get(file.getPath());
                    List<String> result = Files.readAllLines(rTargetPath);
                    String content = "";
                    for (String line : result) {
                        content += '\n' + line;
                    }
                    Path wTargetPath = targetDir.resolve("test").resolve(tempName + "__" + file.getName());
                    Files.write(wTargetPath, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 删除文件和目录
    static void testDelFileOrDir(@Nullable File fileOrDir) {
        if (fileOrDir == null) {
            Path path = ROOT_PATH.resolve("start").resolve("doc").resolve("test");
            fileOrDir = path.toFile();
        }

        // 如果一开始是文件fileOrDirs就是空，不会遍历，直接走到删除；如果是目录才会遍历
        File[] fileOrDirs = fileOrDir.listFiles();
        for (File f : fileOrDirs) {
            if (f.isDirectory()) {
                testDelFileOrDir(f);
            } else {
                f.delete();
                System.out.println("删除\t" + f.getPath());
            }
        }
        fileOrDir.delete();
        System.out.println("删除" + fileOrDir.getPath());
    }

    public static void main(String[] args) {
        // testBufferedReader();

        // doc下创建test目录
        // testFileStreamByBuffer("fqmyysjjd");
        // testFileStreamByFiles("fqmyysjjd");
        // testDelFileOrDir(null);

        // 测试序列化
        TestSerializer testSerializer = new TestSerializer();
        String path = "start/doc/test/fqmyysjjd.txt";
        testSerializer.testSerialize(path);
        testSerializer.testDeserialize(path);
    }
}


class TestSerializer implements Serializable {
    String fqmyysjjd;

    String fqmyybjjzw;

    String fqmyybtd;

    String fqmyybjjzj;

    public TestSerializer() {
        this.fqmyysjjd = "fqmyysjjd";
        this.fqmyybjjzw = "fqmyybjjzw";
        this.fqmyybtd = "fqmyybtd";
        this.fqmyybjjzj = "fqmyybjjzj";
    }

    public void testSerialize(String path) {
        try (FileOutputStream fos = new FileOutputStream(path);
             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public TestSerializer testDeserialize(String path) {
        try (FileInputStream fis = new FileInputStream(path);
             ObjectInputStream ois = new ObjectInputStream(fis)) {
            TestSerializer testSerializer = (TestSerializer) ois.readObject();
            System.out.println("反序列化对象");
            System.out.println(testSerializer);
            System.out.println(testSerializer.fqmyysjjd);
            System.out.println(testSerializer.fqmyybjjzw);
            System.out.println(testSerializer.fqmyybtd);
            System.out.println(testSerializer.fqmyybjjzj);
            return testSerializer;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

}